/*
 * Copyright 2007 Matt Jensen
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
package org.jtell.spring;

import org.jtell.JTell;
import org.jtell.JTellContext;
import org.jtell.JTellContextBuilder;
import org.jtell.config.annotation.AnnotationConfigurationSource;
import org.jtell.internal.DefaultEventSinkSource;
import org.jtell.internal.Guard;

/**
 * <p>
 * <code>ContextUtils</code> provides utility methods for dealing with the JTell context.
 * </p>
 */
public class ContextUtils
{
    /**
     * <p>
     * Construct a {@link ContextUtils} instance.
     * </p>
     */
    private ContextUtils()
    {
        super();
    }

    /**
     * <p>
     * Apply default setup items to an existing {@link JTellContextBuilder} instance. This method should apply only
     * those items which will be common to <em>all</em> Spring-based JTell contexts.
     * </p>
     *
     * @param contextBuilder the context builder instance.
     * @return {@link JTellContextBuilder} the supplied context builder instance.
     */
    public static JTellContextBuilder applyBasicSetup(final JTellContextBuilder contextBuilder)
    {
        Guard.notNull("contextBuilder", contextBuilder);
        contextBuilder.addEventSinkSource(new DefaultEventSinkSource());
        return contextBuilder;
    }

    /**
     * <p>
     * Create a context with default settings.
     * </p>
     *
     * @return {@link JTellContext} instance.
     */
    public static JTellContext createDefaultContext()
    {
        final JTellContextBuilder contextBuilder =
                JTell.buildContext().setConfigurationSource(new AnnotationConfigurationSource());
        applyBasicSetup(contextBuilder);
        return contextBuilder.createContext();
    }
}
